package com.woyowodaows.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.woyowodaows.R;
import com.woyowodaows.activity.home.ShowPictureActivity;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.bean.home.RepairDetailBean;
import com.woyowodaows.bean.home.RepairDetailData;
import com.woyowodaows.bean.mine.ImageUrlBean;
import com.woyowodaows.dialog.ConfirmOneButtonDialog;
import com.woyowodaows.dialog.ConfirmTwoButtonDialog;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.ImageLoaderUtlis;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.RatingBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/28.
 * 订单详情
 */

public class OrderInformationActivity extends BaseActivity {
    @BindView(R.id.tv_order_tile)
    TextView tvOrderTile;
    @BindView(R.id.tv_order_state)
    TextView tvOrderState;
    @BindView(R.id.tv_order_serial_number)
    TextView tvOrderSerialNumber;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_take_order_time)
    TextView tvOrderTakeOrderTime;
    @BindView(R.id.tv_detailed_description)
    TextView tvDetailedDescription;
    @BindView(R.id.iv_order_malfunction)
    ImageView ivOrderMalfunction;
    @BindView(R.id.tv_order_linkman)
    TextView tvOrderLinkman;
    @BindView(R.id.tv_order_phone)
    TextView tvOrderPhone;
    @BindView(R.id.tv_order_area)
    TextView tvOrderArea;
    @BindView(R.id.tv_order_detailed_address)
    TextView tvOrderDetailedAddress;
    @BindView(R.id.tv_order_remark)
    TextView tvOrderRemark;
    @BindView(R.id.rb_order_logistics)
    RatingBar rbOrderLogistics;
    @BindView(R.id.tv_order_score)
    TextView tvOrderScore;
    @BindView(R.id.ll_order_evaluate)
    LinearLayout llOrderEvaluate;
    @BindView(R.id.tv_order_evaluate_time)
    TextView tvOrderEvaluateTime;//评价时间
    @BindView(R.id.tv_order_contens)
    TextView tvOrderContens;
    private RepairDetailBean repairDetailBean;
    private String OrderSn;
    private ConfirmOneButtonDialog confirmOneButtonDialog;

    private ConfirmTwoButtonDialog onfirmTwoButtonDialog;

    private String content;//评价内容

    private float grade = 0;
    private ArrayList<ImageUrlBean> imageUrls;//故障图片

    public float evaluationService = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_information;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "订单详情", "", 0, 0, null);
        if (getIntent() != null) {
            OrderSn = this.getIntent().getStringExtra("OrderSn");
        }
        imageUrls = new ArrayList<>();
        getData();


    }

    @Override
    public void initEvent() {
        super.initEvent();
        tvOrderState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (repairDetailBean.getStatus()) {
                    case -1:
                        tvOrderState.setText("过期关闭");
                        break;
                    case 0:
                        tvOrderState.setText("订单取消");
                        break;
                    case 1:
                        tvOrderState.setText("待接单");
                        if (repairDetailBean.getFlag().equals("repair")) {// user
                            confirmOneButtonDialog = new ConfirmOneButtonDialog(mContext);
                            confirmOneButtonDialog.setTitileAndButtonName("确定接单吗？", "确定");
                            confirmOneButtonDialog.setOneButtonListener(new ConfirmOneButtonDialog.OnOneButtonListener() {
                                @Override
                                public void onConfirmButton() {
                                    getOrderReceiving();
                                }
                            });
                            confirmOneButtonDialog.showDialog();
                        } else {
                            ToastUtil.showWarning(mContext, "您还未有该权力");
                        }
                        break;
                    case 3:
                        tvOrderState.setText("已接单");
                        if (repairDetailBean.getFlag().equals("repair")) {// user
                            confirmOneButtonDialog = new ConfirmOneButtonDialog(mContext);
                            confirmOneButtonDialog.setTitileAndButtonName("确定已经维修吗？", "确定");
                            confirmOneButtonDialog.setOneButtonListener(new ConfirmOneButtonDialog.OnOneButtonListener() {
                                @Override
                                public void onConfirmButton() {
                                    getOrderHaveMaintenance();
                                }
                            });
                            confirmOneButtonDialog.showDialog();
                        } else {
                            ToastUtil.showWarning(mContext, "您还未有该权力");
                        }

                        break;
                    case 4:

                        //gradeStatus` int(1) NOT NULL DEFAULT '0' COMMENT '0 待评价 1已评价',
                        if (repairDetailBean.getFlag().equals("user")) {//用户评价
                            if (repairDetailBean.getGradeStatus() == 0) {
                                onfirmTwoButtonDialog = new ConfirmTwoButtonDialog(mContext);
                                onfirmTwoButtonDialog.setTwoButtonListener(new ConfirmTwoButtonDialog.OnTwoButtonListener() {
                                    @Override
                                    public void onConfirmButton() {
                                        content = onfirmTwoButtonDialog.getDataDiscuss();//评价内容
                                        if (content.equals("")) {
                                            ToastUtil.show(mContext, "输入评价的内容不能为空");
                                            return;
                                        }
                                        grade = onfirmTwoButtonDialog.getGrade();
                                        getEvaluate();//用户评价
                                    }
                                });
                                onfirmTwoButtonDialog.showDialog();
                                tvOrderState.setText("待评价");
                                tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                            } else {
                                tvOrderState.setText("处理完成");
                                tvOrderState.setBackgroundResource(R.drawable.shape_bg_red_small);
                                ToastUtil.showWarning(mContext, "您已经评价过啦");
                            }
                        }

                        break;
                }
            }
        });

        ivOrderMalfunction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageUrls == null || imageUrls.size() == 0) {
                    return;
                }
                Intent intent = new Intent(mContext, ShowPictureActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("imageUrls", imageUrls);
                intent.putExtras(mBundle);
                startActivity(intent);
            }
        });
    }

    private void getData() {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("orderSn", OrderSn);
        showProgressDialog(mContext, "加载中...");
        HttpAsyncTask.getInstance().onPostParam(this, "", false, InterfaceUrl.REPAIRDETAIL, RepairDetailData.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    RepairDetailData bean = (RepairDetailData) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (bean.getData() != null) {
                            repairDetailBean = bean.getData();
                            tvOrderTile.setText(repairDetailBean.getTitle());
//                        repairDetailBean.getStatus()
//                                -1过期关闭 0订单取消  1待接单 3.已接单 4.处理完成',
                            switch (repairDetailBean.getStatus()) {
                                case -1:
                                    tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                                    tvOrderState.setText("过期关闭");
                                    break;
                                case 0:
                                    tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                                    tvOrderState.setText("订单取消");
                                    break;
                                case 1:
                                    tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                                    tvOrderState.setText("待接单");
                                    break;
                                case 3:
                                    tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                                    tvOrderState.setText("已接单");
                                    break;
                                case 4:
                                    if (repairDetailBean.getGradeStatus() == 0) {
                                        tvOrderState.setText("待评价");
                                        tvOrderState.setBackgroundResource(R.drawable.shape_bg_bule_small);
                                    }else {
                                        tvOrderState.setText("处理完成");
                                        tvOrderState.setBackgroundResource(R.drawable.shape_bg_red_small);
                                    }
                                    break;
                            }
                        }
                        tvOrderSerialNumber.setText(repairDetailBean.getOrderSn());//订单编号
                        tvOrderTime.setText(repairDetailBean.getCreateTime());//报修时间
                        tvOrderTakeOrderTime.setText(repairDetailBean.getReceiveTime());//接单时间
                        tvDetailedDescription.setText(repairDetailBean.getDetail());//详细说明
                        //联系人    contacts
                        tvOrderLinkman.setText(repairDetailBean.getContacts());
                        // 手机号码
                        tvOrderPhone.setText(repairDetailBean.getPhone());
                        //地区信息
                        tvOrderArea.setText(repairDetailBean.getDistributionArea());
                        //详细地址
                        tvOrderDetailedAddress.setText(repairDetailBean.getAddress());
                        //备注
                        tvOrderRemark.setText(repairDetailBean.getRemark());
                        repairDetailBean.setPic(bean.getFileUrl() + repairDetailBean.getPic());
                        ImageLoaderUtlis.displayImage(mContext, repairDetailBean.getPic(), R.mipmap.default_image, ivOrderMalfunction);
                        ImageUrlBean images = new ImageUrlBean();
                        images.setTitle(repairDetailBean.getTitle());
                        images.setUrls(repairDetailBean.getPic());
                        images.setContens(repairDetailBean.getDetail());
                        imageUrls.add(images);
                        if (repairDetailBean.getGradeStatus() == 1) {
                            //'0 待评价 1已评价',
                           llOrderEvaluate.setVisibility(View.VISIBLE);
                        }else {
                            llOrderEvaluate.setVisibility(View.GONE);
                        }
                        //评价时间
                        tvOrderEvaluateTime.setText(repairDetailBean.getGradeTime());
                        //评价内容
                        if (repairDetailBean.getComment().equals("")){
                            tvOrderContens.setText("暂无");
                        }else {
                            tvOrderContens.setText(repairDetailBean.getComment());
                        }
                        //评价分数
                        tvOrderScore.setText(repairDetailBean.getScore()+"分");
                        evaluationService=Float.valueOf(repairDetailBean.getScore()+"");
                        rbOrderLogistics.setStar(evaluationService);

                    } else {
                        ToastUtil.show_short(mContext, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(mContext, message);
                dismissProgressDialog();
            }
        });

    }

    private void getOrderReceiving() {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("orderSn", OrderSn);
        showProgressDialog(mContext, "加载中...");
        HttpAsyncTask.getInstance().onPostParam(this, "", false, InterfaceUrl.REPAIRRECEIVE, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        ToastUtil.show_short(mContext, bean.getMessage());
                        finish();
                    } else {
                        ToastUtil.show_short(mContext, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(mContext, message);
                dismissProgressDialog();
            }
        });
    }

    private void getOrderHaveMaintenance() {//已经维修
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("orderSn", OrderSn);
        showProgressDialog(mContext, "加载中...");
        HttpAsyncTask.getInstance().onPostParam(this, "", false, InterfaceUrl.REPAIRED, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        ToastUtil.show_short(mContext, bean.getMessage());
                        finish();
                    } else {
                        ToastUtil.show_short(mContext, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(mContext, message);
                dismissProgressDialog();
            }
        });
    }

    public void getEvaluate() {
        Map<String, Object> mapParameter = new HashMap<>();
//        comment:很好
//        score:5 评分1-5分
        mapParameter.put("orderSn", OrderSn);
        mapParameter.put("comment", content);
        mapParameter.put("score", (int) grade + "");
        showProgressDialog(mContext, "加载中...");
        HttpAsyncTask.getInstance().onPostParam(this, "", false, InterfaceUrl.REPAIRCOMMENT, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        ToastUtil.show_short(mContext, bean.getMessage());
                        finish();
                    } else {
                        ToastUtil.show_short(mContext, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(mContext, message);
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
